package com.example.algorithm.sort;

import java.util.Map;

/**
 * 归并排序
 *
 * @author brad zhang
 * @version 2.0
 * @date 2021/10/9 9:52
 */
public class MergeSort {

    static int number = 0;
    public static void main(String[] args){
        for(int i = 0;i<5 ;i++){
            System.out.println("i="+i);
            for (int j = 0;j<5 ;j++){
                if(j==2){
                    break;
                }
                System.out.println("j="+j);
            }
        }
        String a = "123456,78910";
        System.out.println(a.contains("1234567"));
        // int[] a = {26, 5, 98, 108, 28, 99, 100, 56, 34, 1};
        // printArray("排序前: ",a);
        // MergeSort(a);
        // printArray("排序后: ",a);
    }
    public static void printArray(String pre, int[] a){

        System.out.println(pre);
        for(int i=0;i<a.length;i++)
            System.out.print(a[i]+"\t");
        // for (int a1:a){
        //     System.out.print(a1+"\t");
        // }
        System.out.println();
    }

    public static void MergeSort(int[] a){
        System.out.println("开始排序");
        sort(a,0,a.length-1);
    }
    private static void sort(int[] a,int left, int right){
        if(left >= right)
            return;

        int mid = (left + right) / 2;
        sort(a,left,mid);
        sort(a,mid + 1,right);
        merge(a,left,mid,right);
    }

    private static void merge(int[] a,int left,int mid,int right){
        int[] tmp = new int[a.length];
        int r1 = mid + 1;
        int tIndex = left;
        int cIndec = left;
        //逐个归并

        while(left <= mid && r1 <= right){
            if(a[left] >= a[r1])
                tmp[tIndex++] = a[left++];
            else
                tmp[tIndex++] = a[r1++];
        }
        //讲左边剩余归并
        while (left <= mid){
            tmp[tIndex++] = a[left++];
        }
        //右边剩余归并
        while (r1 <= right){
            tmp[tIndex++] = a[r1++];
        }
        System.out.println("第"+(++number)+"躺排序:\t");

        //从临时数据组拷贝到原数据组
        while (cIndec<=right){
            a[cIndec]=tmp[cIndec];
            System.out.print(a[cIndec]+"\t");
            cIndec++;
        }
        System.out.println();
    }
}


